﻿using Authority.Doamin;
using Authority.Doamin.IRepository;
using Authority.Doamin.View;
using HelperFromTheSkeleton.EFBaseRepository;
using HelperFromTheSkeleton.Enums;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System.Linq.Expressions;

namespace Authority.EFRepositoryImpl.EFRepositoryImpl
{
    public class MenuAuthorityRepositoryImpl : EFBaseTreeRepositoryImpl<MenuAuthority>, IMenuAuthorityRepository
    {
        private readonly AuthorityDbContext _authorityDbContext;
        public MenuAuthorityRepositoryImpl(AuthorityDbContext authorityDbContext) : base(authorityDbContext)
        {
            _authorityDbContext = authorityDbContext;
        }

        public void GetPPP(Guid id)
        {
            _authorityDbContext.Database.SqlQueryRaw<MenuAuthority>(@"
                with subqry (ID,Name,ParentID) as(
                select ID,Name,ParentID from [MenuAuthority] where ID= @id 
                union all
                select [MenuAuthority].ID,[MenuAuthority].Name,[MenuAuthority].ParentID from [MenuAuthority],subqry
                where [MenuAuthority].ParentID = subqry.ID
                )select* from subqry;",
                new[] { new SqlParameter("id", id) });
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        public List<MenuView> GetAllMenuView()
        {
            var routingDbSet = _authorityDbContext.RoutingAuthority;
            return _dbSet.Where(m => true).Join(routingDbSet, m => m.RoutingAuthorityID, n => n.ParentID, (m, n) => new MenuView
            {
                DateAddTime = m.DateAddTime,
                ParentID = m.ParentID,
                ID = m.ID,
                Code = m.Code,
                DateUpTime = m.DateUpTime,
                IsDelete = m.IsDelete,
                Name = m.Name,
                Routing = n.Address,
                SubsystemID = m.SubsystemID,
            }).ToList();
        }

        /// <summary>
        /// 查找菜单
        /// </summary>
        public async Task<List<MenuView>> FindMenuView(Expression<Func<MenuAuthority, bool>> expression)
        {
            var routingDbSet = _authorityDbContext.RoutingAuthority;
            return await (from menuDB in _dbSet.Where(expression)
                          join routingAuthorityDB in routingDbSet on menuDB.RoutingAuthorityID equals routingAuthorityDB.ID into routingAuthoritys
                          from routingAuthorityDF in routingAuthoritys.DefaultIfEmpty()
                          select new MenuView
                          {
                              DateAddTime = menuDB.DateAddTime,
                              ParentID = menuDB.ParentID,
                              ID = menuDB.ID,
                              Code = menuDB.Code,
                              DateUpTime = menuDB.DateUpTime,
                              IsDelete = menuDB.IsDelete,
                              Name = menuDB.Name,
                              Routing = routingAuthorityDF.Address,
                              SubsystemID = menuDB.SubsystemID,
                              RoutingID = routingAuthorityDF.ID
                          }).ToListAsync();
        }
    }
}
